package com.example.base.jsonserializer;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * 手机号脱密
 */
public class PhoneSensitiveInfoSerializer implements ObjectSerializer {

    @Override
    public void write(JSONSerializer jsonSerializer, Object object, Object o1, Type type, int i) throws IOException {
        SerializeWriter out = jsonSerializer.out;
        if (object == null) {
            out.writeNull();
            return;
        }
        String phoneNumber = (String) object;
        String desensitizedPhoneNumber = phoneNumber.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        out.writeString(desensitizedPhoneNumber);
    }
}
